********************************************************************************
*Replication Data for: Unpopular Protest: Mass Mobilization and Attitudes to Democracy in Post-Mubarak Egypt
*NB. The analysis uses a restricted version of Wave II of the Arab Barometer. To access 
*the survey data contact the Arab Barometer's technical support staff using the form on the 
*website: https://www.arabbarometer.org/contact/. The relevant file is EGYPT2_local
*and includes geographic variables that allow respondents to be assigned to the district level.
********************************************************************************
cap cd "C:\Users\neilke\Dropbox\local protest and attitudes towards democracy\production\replication_files"
cap cd "C:\Users\TELRAYYES\Dropbox\local protest and attitudes towards democracy\production\replication_files"


*Read in AB II for Egypt
use EGYPT2_local.dta, clear

********************************************************************************
**********************AB Wave II variables**************************************
********************************************************************************

***DV
recode q5161 q5162 q5163 q5167 (1=0) (2=1) (3=3) (4=4) (*=2), gen(dem_supp1 dem_supp2 dem_supp3 dem_supp7)
egen attitude_to_democracy=rmean(dem_supp1 dem_supp2 dem_supp3 dem_supp7)
alpha dem_supp1 dem_supp2 dem_supp3 dem_supp7

***Controls

*Age and age squared
rename q1001 age
gen age_squared = age^2

*Level of education
tab q1003
recode q1003 (1=1) (2=2) (3=3) (4=4) (5=5) (6=6) (7=7) (*=.), gen(educ)

*Male
tab q1002
recode q1002 (1=1) (2=0) (*=.), gen(male)

*Employment
recode q1005(1  2=1 "Out of employment")(3=2 "Student")(5=3 "Unemployed")(.=4 "Employed")(*=.), into(empcat1)

*Employment with employed as base category
recode q1005(1=4 "Out of employment")(2=4 "Out of employment")(3=2 "Student")(5=3 "Unemployed")(.=1 "Employed")(*=.), into(empcat)

*Urban
recode q4 (1=1) (2=0), gen(urban)

*Muslim
recode q1012 (1=1) (2=0) (3=0) (*=.), gen(muslim)

*Religiosity (frequency of reading religious texts)
recode q6106 (1=5) (2=4) (3=3) (4=2) (5=1) (*=.), gen(religiosity)

*Log of income to base 10
gen log10_income=log10(q1015)

*Business owner
gen business_owner = 1 if q1007 == 1 | q1007 == 2
replace business_owner = 0 if business_owner ==.

*Supported Mubarak during 25 Jan Revolution
recode EG801 (1=1) (2=0) (3=0) (8=0), gen(support_mubarak)

*Protested during 25 Jan Revolution
recode EG802 (1=1) (2=0), gen(participated_jan_rev)

*Offered assistance to protestors during 25 Jan Revolution
recode EG804 (1=1) (2=0) (*=.), gen(aided_protesteors)

*Protested during post-Mubarak transition
recode EG812 (2=0) (1=1) (0=0), gen(protest_tranisition)

*Voted in the past?
recode q301 (1=1) (2=0) (3=0) (*=.), gen(vote_past)

*Gets news from state media or counter-revolutionary media
decode q408, gen(media) 
gen state_media = 1 if media=="الأولى" | media == "البرنامج العام في الإذاعة" | media == "النيل للأخبار" ///
	| media == "النيل للرياضة" | media == "الفراعين"  ///
	| media == "الأهرام" | media == "الفضائية المصرية" | media == "جريدة الجمهورية" ///
	| media == "محطة مصر" | media == "القناة الثانية" | media == "جريدة الأخبار" ///
	| media == "fm إذاعة" 
replace state_media = 0 if state_media ==.
	
*Level of democracy in Egypt at time of survey
recode q511 (0=0) (1=1) (2=2) (3=3) (4=4) (5=5) (6=6) (7=7) (8=8) (9=9) (10=10) (*=.), gen(dem_assessment)	
	
***Additional variables to test in robustness

*Plans to vote in parliamentary elections
recode q302a (1=1) (2=0) (3=0) (*=.), gen(will_vote_parl) 

*Will vote for the Muslim Brotherhood in parliamentary elections
gen will_vote_mb_supporter = 1 if EG308A == 602
replace will_vote_mb_supporter = 0 if will_vote_mb_supporter ==.

********************************************************************************
***********************Merge district variables*********************************
********************************************************************************

merge 1:1 qid using ketchley_elrayyes_jop.dta
drop _merge
save full_dataset.dta, replace

********************************************************************************
***********************Macros for analysis**************************************
********************************************************************************

*Dependent variables
global y_basic attitude_to_democracy
global y_alt miss_attitude_to_democracy
global y1list dem_supp1
global y2list dem_supp2
global y3list dem_supp3
global y4list dem_supp7

*Protest variables
global protest sqrt_all_protest_events
global ppart_protest sqrt_all_ppart
global events_by_sector sq_lab_events sq_res_events sq_students_events sq_activist_events
global lab_repertoire sq_labdemo sq_labstrike sq_labsitin
global all_repertoire sq_alldemo sq_allstrike sq_allsitin
global ppart_repertoire sq_sittin_ppart sq_strike_ppart sq_demo_ppart

*Instrumental variable
global instrument sqrt_distance_to_focal_point

*Main analysis controls
global controls age age_squared educ ///
	male ib3.empcat1 ///
	muslim religiosity log10_income business_owner ///
	pre_rev_protest_in_district sqrt_killed_25_jan urban pop_density_thousands sqrt_distance_from_cairo police_station_attacked ///
	vote_past dem_assessment  ///
	support_mubarak participated_jan_rev aided_protesteors protest_tranisition ///
	state_media		
	
*Robustness controls -- ICC
global alt_controls age age_squared educ ///
	male empcat1 ///
	muslim religiosity log10_income business_owner ///
	pre_rev_protest_in_district sqrt_killed_25_jan urban pop_density_thousands sqrt_distance_from_cairo police_station_attacked ///
	vote_past dem_assessment  ///
	support_mubarak participated_jan_rev aided_protesteors protest_tranisition ///
	state_media		
	
*Robustness controls -- weighted LS
global alt_controls_2 age age_squared educ ///
	male i.empcat ///
	muslim religiosity log10_income business_owner ///
	pre_rev_protest_in_district sqrt_killed_25_jan urban pop_density_thousands sqrt_distance_from_cairo police_station_attacked ///
	vote_past dem_assessment  ///
	support_mubarak participated_jan_rev aided_protesteors protest_tranisition ///
	state_media		

********************************************************************************
*Fn.4: Difference between survey 3rd and 4th survey items !=0?
********************************************************************************
*p=.82
ttest dem_supp3 == dem_supp7
	
********************************************************************************
*Fn.6: ICC -- variance at governorate level
********************************************************************************
*11% of variatiation in attitudes to democracy is due to between-governorate variation
*LR = 82.69 (p=.000)
egen nmiss = rowmiss ($y_basic $alt_controls $protest)
mixed $y_basic || q1: , mle variance, if nmiss==0
estat icc	
	
********************************************************************************
********************************Table 1 & Figure 4******************************
**********Effect of distance to focal point on protest in district**************
********************************************************************************

*Model 1
xi: regress $protest ///
	$instrument, vce(cluster q2) level(90)
	
*Model 2 
xi: regress $protest ///
	$instrument i.q1, vce(cluster q2) level(90)
	
*Model 3
xi: regress $protest ///
	$instrument $controls, vce(cluster q2) level(90)
	
*Model 4	
xi: regress $protest ///
	$instrument $controls i.q1, vce(cluster q2) level(90)

*Figure 4
margins, at($instrument =(0(1)10)) atmeans level(90)
marginsplot, scheme(538w) recast(line) recastci(rline) ///
	xtitle("District distance to focal point (sqrt, km)", size(medsmall)) ///
	ytitle("Protest events in district (sqrt)", size(medsmall)) ///
	title("") ylab(0 2 4 6 8) aspectratio(1) legend(off) ///
	addplot(lowess $protest $instrument, bwidth(0.6) ylab(0 2 4 6 8) lcolor(red) lpattern(dash) ///
	|| scatter $protest $instrument [w=respondent], ylab(0 2 4 6 8))  

*margins at 5th and 95th percentiles
quietly summ $instrument, det
local p5 = r(p5)
local p95 = r(p95)

margins, at($instrument = `p5') atmeans level(90)	
margins, at($instrument = `p95') atmeans level(90)		

*percent decrease from 5-95th percentiles
*73%
di ((2.615-0.698)/2.615)*100

********************************************************************************
********************************Table 2*****************************************
*****Effect of distance to focal point on district's political composition******
********************************************************************************

*Model 5
regress NDP_elected_2005 ///
	$instrument, vce(cluster q2)

*Model 6
reg NDP_strong ///
	$instrument, vce(cluster q2)
	
*Model 7
reg MB_elected_2005  ///
	$instrument, vce(cluster q2)	
 
********************************************************************************
********************************Table 3 & Figure 5******************************
***************Effect of protest on attitudes to democracy**********************
********************************************************************************

*Model 8
xi: regress $y_basic ///
	$protest ///
	$controls ///
	i.q1, vce(cluster q2) level(90)	

*Model 8 margins from 5th to 95th percentile
*5th percentile = 0; 95th percentile = 5
summ $protest if nmiss==0, det
local p5 = r(p5)
local p95 = r(p95)

margins, at($protest = `p5') atmeans level(90) 
margins, at($protest = `p95') atmeans level(90) 

*8.5% reduction in attitudes 
di %9.1f ((  2.835 - 2.595 )/  2.835)*100 

*Model 9
xi: ivregress 2sls $y_basic ///
	$controls ///
	i.q1 ///
	($protest = $instrument), ///
	vce(cluster q2) first level(90)		
	
estat first
		
*Model 9 margins from 5th to 95th percentile
*5th percentile = 0; 95th percentile = 5
summ $protest if nmiss==0, det
local p5 = r(p5)
local p95 = r(p95)
local p99 = r(p99)

margins, at($protest = `p5') atmeans level(90) 
margins, at($protest = `p95') atmeans level(90) 
margins, at($protest = `p99') atmeans level(90) 
	
*percent decrease from 5-95th percentiles
*24.2% reduction
di %9.1f (( 2.987-2.265)/ 2.987 )*100

*percent decrease from 5-99th percentiles
*36.1% reduction
di %9.1f (( 2.987 -1.910)/ 2.987 )*100	
	
*Figure 5
xi: ivregress 2sls $y_basic ///
	$controls ///
	i.q1 ///
	($protest = $instrument), ///
	vce(cluster q2) first level(90)
	
gen xaxisrug = -0.003
gen rug = "|"	

margins, at($protest =(0(1)9)) atmeans level(90)

marginsplot, ylabel(0(1)4) ytitle("Attitude to democracy index (0-4)", size(med)) ///
	xtitle("Protest events in district (sqrt)", size(med)) title("") ///
	aspectratio(1) ///
	addplot(scatter xaxisrug $protest, ms(none) mlabel(rug) mlabpos(0) ylabel(0(1)4) xlabel(0 1 2 3 4 5 6 7 8 9)) legend(off)

********************************************************************************
*Fn.10 -- IV regression diagnostics
********************************************************************************
ivreg2 $y_basic ///
	$controls ///
	i.q1 ///
	($protest = $instrument), ///
	cluster(q2) first level(90)	partial(i.q1)		
	
********************************************************************************	
*Fn.13 -- Decompose DV and test blockades, occupations, sit-ins
********************************************************************************
xi: reg $y1list ///
	$controls ///
	sq_allsitin i.q1, vce(cluster q2) level(90) 	
		
xi: reg $y2list ///
	$controls ///
	sq_allsitin i.q1, vce(cluster q2) level(90) 	

xi: reg $y3list ///
	$controls ///
	sq_allsitin i.q1, vce(cluster q2) level(90) 	
	
xi: reg $y7list ///
	$controls ///
	sq_allsitin i.q1, vce(cluster q2) level(90) 	
	
********************************************************************************
*Fn.15.: Protest and future voting intentions
********************************************************************************

logit will_vote_parl ///
	$protest ///
	vote_past, ///
	vce(cluster q2) level(90) or
		
********************************************************************************
*********************************APPENDIX***************************************
********************************************************************************

*Appendix figure 1
hist $y_basic, ///
		discrete percent
	
********************************************************************************
******************************APPENDIX Tables A1 & A2***************************
********************Descriptive statistics and correlation matrix***************
********************************************************************************

*Table A1 -- Descriptive statistics
tabstat $y_basic $protest $instrument $alt_controls ///
	, col(stat) stat(mean sd min max) format(%9.2f)	
	
*Table A2 -- Correlation matrix	
corr $y_basic $protest $alt_controls	
		
********************************************************************************
********************************Appendix Table A3 & A4**************************
**********************************Decomposing DV********************************
********************************************************************************	

*Model 10	
xi: regress $y1list ///
	$protest ///
	$controls ///
	i.q1, vce(cluster q2) level(90)	

*Model 11		
xi: regress $y2list ///
	$protest ///
	$controls ///
	i.q1, vce(cluster q2) level(90)	

*Model 12		
xi: regress $y3list ///
	$protest ///
	$controls ///
	i.q1, vce(cluster q2) level(90)	

*Model 13		
xi: regress $y4list ///
	$protest ///
	$controls ///
	i.q1, vce(cluster q2) level(90)		
	
*Model 14
xi: ivregress 2sls $y1list ///
	$controls ///
	i.q1 ///
	($protest = $instrument), ///
	vce(cluster q2) first level(90)
		
*Model 15
xi: ivregress 2sls $y2list ///
	$controls ///
	i.q1 ///
	($protest = $instrument), ///
	vce(cluster q2) first level(90)
	
*Model 16
xi: ivregress 2sls $y3list ///
	$controls ///
	i.q1 ///
	($protest = $instrument), ///
	vce(cluster q2) first level(90)
	
*Fn.11.
margins, at($protest =(0(1)9)) atmeans level(90)
	
*Model 17
xi: ivregress 2sls $y4list ///
	$controls ///
	i.q1 ///
	($protest = $instrument), ///
	vce(cluster q2) first level(90)	
	
*Fn.11.
margins, at($protest =(0(1)9)) atmeans level(90)	
	
********************************************************************************
********************************Appendix Table A5*******************************
***************************************MI***************************************
********************************************************************************
preserve
use full_dataset, clear

*Dependent variables
global y_basic attitude_to_democracy

*Protest variables
global protest sqrt_all_protest_events

*Instrumental variable
global instrument sqrt_distance_to_focal_point

*Robustness controls -- ICC
global alt_controls age age_squared educ ///
	male empcat1 ///
	muslim religiosity log10_income business_owner ///
	pre_rev_protest_in_district sqrt_killed_25_jan urban pop_density_thousands sqrt_distance_from_cairo police_station_attacked ///
	vote_past dem_assessment  ///
	support_mubarak participated_jan_rev aided_protesteors protest_tranisition ///
	state_media		
	
mi set mlong
*variables to impute if missing
mi misstable summarize ///
	$alt_controls
	
*missing values
mi register imputed ///
	educ empcat1 muslim log10_income vote_past aided_protesteors dem_assessment
	
set seed 29390	
mi impute mvn ///
	educ empcat1 muslim log10_income vote_past aided_protesteors dem_assessment = ///
	total_population_10plus total_employed manufacturingpct publicutilitypct universitypct illiteratepct agriculturalpct ///
	, add(20)
	
*Model 18
mi estimate: ///
	regress $y_basic ///
	$protest ///
	$alt_controls ///
	i.q1, vce(cluster q2) level(90)
	
restore
	
********************************************************************************
********************************Appendix Table A6*******************************
*************************Change in daily rate of protest************************
********************************************************************************

*Model 19
xi: regress $y_basic ///
	change_daily_rate_protest ///
	$controls ///
	i.q1, vce(cluster q2)		
	
*Model 20
xi: ivregress 2sls $y_basic ///
	$controls i.q1 ///
	(change_daily_rate_protest = $instrument) ///
	, vce(cluster q2) first
	
********************************************************************************
********************************Appendix Table A7*******************************
*************************Protest during survey period***************************
********************************************************************************	
	
*Model 21
xi: regress $y_basic ///
	$protest ///
	$controls ///
	post_treatment_protest_events ///
	i.q1, vce(cluster q2) level(90)

*Model 22
xi: ivregress 2sls $y_basic ///
	$controls ///
	post_treatment_protest_events ///
	i.q1 ///
	($protest = $instrument), ///
	vce(cluster q2) first level(90)			

estat first
	
*Model 23
xi: regress $y_basic ///
	sqrt_survey_protest_events_all ///
	$controls ///
	i.q1, vce(cluster q2) level(90)

*Model 24
xi: ivregress 2sls $y_basic ///
	$controls ///
	i.q1 ///
	(sqrt_survey_protest_events_all = $instrument), ///
	vce(cluster q2) first level(90)			
	
estat first
	
********************************************************************************
********************************Appendix Table A8*******************************
**********************************Reduced models********************************
********************************************************************************

*Model 25
pdslasso ///
	$y_basic ///
	$protest($controls i.q1) ///
	, partial(i.q1) olsoptions(cluster(q2)) rlasso 

*Model 26
ivlasso ///
	$y_basic ///
	($controls i.q1) ///
	($protest = $instrument) ///
	, ivoptions(cluster(q2)) partial(i.q1) first  	

*Model 27
xi: stepwise, pr(.05) lockterm1: ///
	regress $y_basic ///
	$protest ///
	$alt_controls_2 ///
	(i.q1), vce(cluster q2) level(90)		
		
	
********************************************************************************
********************************Appendix Table A9*******************************
**********************************Weighted models********************************
********************************************************************************
	
*Model 28
xi: wls0 $y_basic ///
	$protest ///
	$alt_controls_2 ///
	i.q1, wvar(wt) type(abse) robust

*Model 29
xi: wls0 $y_basic ///
	$protest ///
	$alt_controls_2 ///
	i.q1, wvar(wt) type(e2) robust 	

*Model 30
xi: wls0 $y_basic ///
	$protest ///
	$alt_controls_2 ///
	i.q1, wvar(wt) type(loge2) robust 	

*Model 31	
xi: wls0 $y_basic ///
	$protest ///
	$alt_controls_2 ///
	i.q1, wvar(wt) type(xb2) robust 		
	
	
********************************************************************************
********************************Appendix Table A10******************************
********************************Multilevel models*******************************
********************************************************************************

*Model 32
xi: mixed $y_basic ///
	$protest ///
	$controls  ///
	|| q1:, vce(robust) level(90)	

*Model 33
xi: mixed $y_basic ///
	$protest ///
	$controls  ///
	|| q1: || q2:, vce(robust) level(90)		
	
********************************************************************************
********************************Appendix Table A11******************************
**************************Protest events by repertoire**************************
********************************************************************************

corr $all_repertoire

*Model 34
xi: reg $y_basic ///
	$controls ///
	sq_allsitin i.q1, vce(cluster q2) level(90) 	

*Model 35
xi: reg $y_basic ///
	$controls ///
	sq_alldemo i.q1, vce(cluster q2) level(90) 	

*Model 36
xi: reg $y_basic ///
	$controls ///
	sq_allstrike i.q1, vce(cluster q2) level(90) 	

*Model 37
xi: reg $y_basic ///
	$controls ///
	$all_repertoire i.q1, vce(cluster q2) level(90) 	
		
********************************************************************************
********************************Appendix Table A12*****************************
*****************************Protest events by sector***************************
********************************************************************************

*Model 38
xi: reg $y_basic ///
	$controls ///
	sq_lab_events i.q1, vce(cluster q2) level(90) 	

*Model 39
xi: reg $y_basic ///
	$controls ///
	sq_res_events i.q1, vce(cluster q2) level(90) 	
	
*Model 40
xi: reg $y_basic ///
	$controls ///
	sq_students_events i.q1, vce(cluster q2) level(90) 	
	
*Model 41
xi: reg $y_basic ///
	$controls ///
	sq_activist_events i.q1, vce(cluster q2) level(90) 	
		
*Model 42
xi: reg $y_basic ///
	$controls ///
	$events_by_sector i.q1, vce(cluster q2) level(90) 	

********************************************************************************
********************************Appendix Table A13******************************
**************************Labor events by repertoire****************************
********************************************************************************
	
*Model 43
xi: reg $y_basic ///
	$controls ///
	sq_labsitin i.q1, vce(cluster q2) level(90)
		
*Model 44
xi: reg $y_basic ///
	$controls ///
	sq_labdemo i.q1, vce(cluster q2) level(90)

*Model 45
xi: reg $y_basic ///
	$controls ///
	sq_labstrike i.q1, vce(cluster q2) level(90)
	
*Model 46
xi: reg $y_basic ///
	$controls ///
	$lab_repertoire i.q1, vce(cluster q2) level(90)
	
********************************************************************************
********************************Appendix Table A14******************************
*****************************Protest participation******************************
********************************************************************************	

*Model 47
xi: regress $y_basic ///
	$controls ///
	$ppart_protest ///
	i.q1, vce(cluster q2) level(90)
	
*Model 48	
xi: ivregress 2sls $y_basic ///
	$controls ///
	i.q1 ///
	($ppart_protest = $instrument), ///
	vce(cluster q2) first level(90)		

estat first	
	
*Model 49	
xi: regress $y_basic ///
	$controls ///
	sq_sittin_ppart ///
	i.q1, vce(cluster q2) level(90)		

*Model 50	
xi: regress $y_basic ///
	$controls ///
	sq_demo_ppart ///
	i.q1, vce(cluster q2) level(90)		
	
*Model 51		
xi: regress $y_basic ///
	$controls ///
	sq_strike_ppart ///
	i.q1, vce(cluster q2) level(90)			

*Model 52
xi: regress $y_basic ///
	$controls ///
	$ppart_repertoire ///
	i.q1, vce(cluster q2) level(90)		
		
********************************************************************************
********************************Appendix Table A15******************************
***********************The effect of protest on Shafiq vote*********************
********************************************************************************			

use appendix_table_13.dta, clear

*Model 53
xi: reg shafiq_pct ///
	as_protest NDP_2005_strong ///
	i.gov_ID, vce(robust) 
	
listcoef as_protest
